package com.dps.recommend.layer.merage;


import com.dps.recommend.ann.LayerAnn;
import com.dps.recommend.beans.DpsRequest;
import com.dps.recommend.beans.ProcessInterface;
import com.dps.recommend.beans.RecItemDps;
import com.dps.recommend.enums.LayerEnum;
import com.dps.recommend.utils.DpsBaseService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * 融合 根据itemId+type去重复
 */
@Service
@LayerAnn(layer = LayerEnum.MERAGE)
public class DpsItemIdTypeV1Merage extends DpsBaseService implements ProcessInterface {


    @Override
    public <R extends RecItemDps> List doProcess(DpsRequest dpsRequest, List<R> list) {
        if (CollectionUtils.isEmpty(list)) {
            return list;
        }
        list = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
                new TreeSet<>(Comparator.comparing(rec -> {
                    return rec.getItemId() + "_" + rec.getType();
                }))), ArrayList::new));

        return list;
    }
}
